import React, {Component, Fragment} from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from "mobx";
import {
    WeaLocaleProvider,
    WeaFormItem,
    WeaSearchGroup,
    WeaSelect,
    WeaHelpfulTip,
} from "ecCom";
import {WeaSwitch} from 'comsMobx';
import {Button} from "antd";
import _ from 'lodash';

import * as IconConstant from '../../constant/IconConstant.js';
import LanguageConstant from '../../constant/LanguageConstant.js';
import CommonConfig from '../../constant/CommonConfig.js';

const {getLabel} = WeaLocaleProvider;

@inject("systemSettingStore")
@observer
class BaseSystemSettingComponent extends Component {

    componentDidMount() {
        const {systemSettingStore} = this.props;
        systemSettingStore.getBaseSystemSettingForm();
    }

    render() {
        const {systemSettingStore} = this.props;
        const {authorized, loading} = systemSettingStore;
        return (
            <div className="base-system-setting">
                <Fragment>
                    {this.baseSystemSettingFormContent()}
                </Fragment>
            </div>
        );
    }

    baseSystemSettingFormContent = () => {
        const {systemSettingStore} = this.props;
        const {
            baseSystemSettingForm, baseSystemSettingFormConditions, pdfConversionType, onSelectChange, syncWorkflowMouldTypeField,
            synchronizing,
        } = systemSettingStore;
        const {isFormInit} = baseSystemSettingForm;
        let group = [];
        let col = 1;
        isFormInit && toJS(baseSystemSettingFormConditions).map(c => {
            let items = [];
            c.items.map(field => {
                const {otherParams, viewAttr, helpfulTip, options, domkey} = field;
                if (otherParams && otherParams.linkageField === 'pdfConversionType') {
                    if (pdfConversionType != otherParams.linkageVal) {
                        return;
                    }
                }
                if (field.conditionType === 'SELECT' && domkey.includes("pdfConversionType")) {
                    items.push({
                        com: (
                            <WeaFormItem
                                label={`${field.label}`}
                                labelCol={{span: `${6}`}}
                                ratio1to2
                                wrapperCol={{span: `${18}`}}>
                                <div className="base-system-setting-pdfConversionType">
                                    <WeaSelect
                                        options={options}
                                        value={"" + pdfConversionType}
                                        onChange={value => onSelectChange("pdfConversionType", value)}
                                    />
                                    <WeaHelpfulTip
                                        title={helpfulTip}
                                    />
                                </div>
                            </WeaFormItem>),
                        col
                    });
                } else if (domkey.includes("syncWorkflowMouldTypeField")) {
                    items.push({
                        com: (<WeaFormItem
                            label={`${field.label}`}
                            labelCol={{span: `${6}`}}
                            ratio1to2
                            tipPosition="bottom"
                            wrapperCol={{span: `${18}`}}>
                            <div className="base-system-setting-syncWorkflowMouldTypeField">
                                <Button type="primary" size="small" disabled={synchronizing || viewAttr == 1}
                                        onClick={() => syncWorkflowMouldTypeField()}>{field.label}</Button>
                                <span style={{"marginLeft": "20px"}}>
                                    <WeaHelpfulTip title={helpfulTip}/>
                                </span>
                            </div>
                        </WeaFormItem>),
                        col: col,
                    });
                } else {
                    items.push({
                        com: (<WeaFormItem
                            label={`${field.label}`}
                            labelCol={{span: `${6}`}}
                            ratio1to2
                            tipPosition="bottom"
                            wrapperCol={{span: `${18}`}}>
                            <WeaSwitch fieldConfig={field} form={baseSystemSettingForm}/>
                        </WeaFormItem>),
                        col: col,
                    });
                }
            });
            group.push(<WeaSearchGroup needTigger={true} title={c.title} showGroup={true} items={items}/>)
        });
        return group;
    }


}

export default BaseSystemSettingComponent;